<?php
/**
* 分页类
*
* Copyright(c) 2012 by Tony. All rights reserved
* To contact the author write to {@link mailto:taonzy@163.com}
* @author Tony
*/
class Page {
    private $totalNum = '';                  //总条数
    private $limit = '';                     //limit
    private $pageSize = '';                  //每页显示几条
    private $totalPageNum = '';              //总页码
    private $url = '';                       //url
    private $bothSidesNum = '';              //两边保持数字分页的量
    private $currentPage = '';               //当前页
    private $prevPage = '';                  //上一页
    private $nextPage = '';                  //下一页
    

    public function __construct($totalNum,$pageSize) {
        $this->totalNum = $totalNum ? $totalNum : 1;
        $this->pageSize = $pageSize;
        $this->totalPageNum = ceil($this->totalNum / $this->pageSize);
        $this->currentPage = $this->getCurrentPage();
        $this->prevPage = $this->currentPage - 1;
        $this->nextPage = $this->currentPage + 1;
        $this->limit = ($this->currentPage-1)*$this->pageSize.",$this->pageSize";
        $this->url= $this->getUrl();
        $this->bothSidesNum = 2;
    }
    //获取limit
    public function getLimit() {
        return $this->limit;
    }
    //获取当前页码
    public function getCurrentPage() {
        if(!empty($_GET['page'])){
            if($_GET['page'] > 0){
                if($_GET['page'] > $this->totalPageNum){
                    return $totalPageNum;
                }else{
                    return $_GET['page'];
                }
            }else{
                return 1;
            }
        }else{
            return 1;
        }
    }
    //获取地址,并去掉url中的page参数
    private function getUrl() {
        $url = $_SERVER['REQUEST_URI'];
        $par = parse_url($url);
        if(isset($par['query'])){
            parse_str($par['query'],$query);
            unset($query['page']);
            $url = $par['path'].'?'.http_build_query($query);
        }
        return $url;
    }
    //数字目录
	private function pageList() {
        $pageList = '';
        for($i = $this->bothSidesNum;$i >= 1;$i--){
            if($this->currentPage-$i < 1) continue;
            $pageList .= '<li><a href="'.$this->url.'&page='.($this->currentPage-$i).'">'.($this->currentPage-$i).'</a></li>';
        }
        $pageList .= '<li class="active"><a href="'.$this->url.'&page='.$this->currentPage.'">'.$this->currentPage.'</a></li>';
        for($i = 1;$i <= $this->bothSidesNum;$i++){
            if($this->currentPage+$i > $this->totalPageNum) break;
            $pageList .= '<li><a href="'.$this->url.'&page='.($this->currentPage+$i).'">'.($this->currentPage+$i).'</a></li>';
        }
        return $pageList;
	}
    //首页
    private function first() {
        if($this->currentPage > $this->bothSidesNum + 1){
            return '<li><a href="'.$this->url.'&page=1">1</a></li>'.'<li><a>...</a></li>';
        }
        
    }
    //上一页
    private function prev() {
        if($this->currentPage == 1){
           return '<ul><li class="disabled"><a href="javascript:void(0)">上一页</a></li>';
        }
        return '<ul><li><a href="'.$this->url.'&page='.$this->prevPage.'">上一页</a></li>';
    }
    //下一页
    private function next() {
        if($this->currentPage == $this->totalPageNum){
            return '<li class="disabled"><a href="javascript:void(0)">下一页</a></li></ul>';
        }
        return '<li><a href="'.$this->url.'&page='.$this->nextPage.'">下一页</a></li></ul>';
    }
    //尾页
    private function last() {
        if($this->totalPageNum - $this->currentPage > $this->bothSidesNum){
            return '<li><a>...</a></li>'.'<li><a href="'.$this->url.'&page='.$this->totalPageNum.'">'.$this->totalPageNum.'</a></li>';
        }
    }
    //分页信息输出
    public function showPagination() {
        $pagination = '';
        $pagination.=$this->prev();
        $pagination.=$this->first();
        $pagination.=$this->pageList();
        $pagination.=$this->last();
        $pagination.=$this->next();
        return $pagination;
    }
}